using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using TestSystem;
using TestSystem.BLL;
using TestSystem.BLL.Profile;

public partial class UserProfile : System.Web.UI.UserControl
{
    private string userName = "";

    public string UserName
    {
        get { return userName; }
        set { userName = value; }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        Page.RegisterRequiresControlState(this);
    }

    protected override void LoadControlState(object savedState)
    {
        object[] controlState = (object[]) savedState;
        base.LoadControlState(controlState[0]);
        userName = (string) controlState[1];
    }

    protected override object SaveControlState()
    {
        object[] controlState = new object[2];
        controlState[0] = base.SaveControlState();
        controlState[1] = userName;
        return controlState;
    } 

    protected void Page_Load(object sender, EventArgs e)
    {
        avatarUploader.MaxLength = Settings.ProfileSettings.MaxAvatarSize;
        if(!Page.IsPostBack)
        {
            CustomProfile profileCommon = (CustomProfile)HttpContext.Current.Profile;
            if (UserName.Length > 0)
                profileCommon = CustomProfile.GetProfile(UserName);

            avatarUploader.FileUrl = profileCommon.Forum.AvatarUrl;
            txtSignature.Text = profileCommon.Forum.Signature;
            txtFirstName.Text = profileCommon.Personal.FirstName;
            txtLastName.Text = profileCommon.Personal.LastName;
            ddlGenders.SelectedValue = profileCommon.Personal.Gender;
            txtWebsite.Text = profileCommon.Personal.Website;
            txtPhone.Text = profileCommon.Contacts.Phone;
            txtICQ.Text = profileCommon.Contacts.ICQ;
        }
    }

    public void SaveProfile()
    {
        CustomProfile profileCommon = (CustomProfile)HttpContext.Current.Profile;
        if (UserName.Length > 0)
            profileCommon = CustomProfile.GetProfile(UserName);
        profileCommon.Forum.AvatarUrl = avatarUploader.FileUrl;
        profileCommon.Forum.Signature = txtSignature.Text;
        profileCommon.Personal.FirstName = txtFirstName.Text;
        profileCommon.Personal.LastName = txtLastName.Text;
        profileCommon.Personal.Gender = ddlGenders.SelectedValue;
        profileCommon.Personal.Website = txtWebsite.Text;
        if(txtBirthDate.Text.Trim().Length > 0)
            profileCommon.Personal.BirthDate = DateTime.Parse(txtBirthDate.Text);
        profileCommon.Contacts.Phone = txtPhone.Text;
        profileCommon.Contacts.ICQ = txtICQ.Text;
        profileCommon.Save();
    }
}
